#! /bin/bash
# -*- tab-width: 4; -*-
# vi: set sw=2 ts=4:

# Regenerate the ktx2 files with the KTX icons.

# Copyright 2017 The Khronos Group Inc.
# SPDX-License-Identifier: Apache-2.0

# Depth of this script relative to the project root
depth=../..

# This script will only work with my arrangement of workareas unless you change
# the following to what is correct for yours.
ktxspec_dir=../../../KTX-Specification

# Change dir to the testimages folder, the script location...
cd $(dirname $0)

# Ensure generation is not polluted by user environment
unset TOKTX_OPTIONS

function usage() {
  echo "Usage: $0 [--bindir <path/to/tools/bindir>]"
  exit 1
}

function check_exec() {
  if [ -z "$1" -o ! -x "$1" ]; then
    echo "$0: $1 not found."
    exit 1
  fi
}

if [ "$1" = "--bindir" ]; then
  shift
  if [ $# -eq 1 ]; then
    bindir=$1
    toktx=$bindir/toktx
    shift
    check_exec $toktx
  else
    usage
  fi
fi

if [ $# -gt 0 ]; then
  usage
fi

function check_path() {
  if which $1 >/dev/null; then
    #declare -g $1="$1"  # For recent versions of bash.
    eval "$1=\$1"
  else
    echo "$1 not found in $PATH."
    exit 1
  fi
}

if [ -z "$toktx" ]; then
  check_path toktx
fi

# Generate ktx2 files of KTX icons.

# These commands will only work with my arrangement of workareas.
$toktx --test --genmipmap --bcmp -scale 0.5 ../webgl/libktx-webgl/ktx_app_basis.ktx2 $ktxspec_dir/icons/png/ktx_app.png
$toktx --test --uastc --uastc_rdo_l 5.0 --resize 1000x1392 ../webgl/libktx-webgl/ktx_document_uastc_rdo5.ktx2 $ktxspec_dir/icons/png/ktx_document.png
$toktx --test --genmipmap -bcmp ktx_document_basis.ktx2 $ktxspec_dir/icons/png/ktx_document.png
# threads 1 here is to avoid non-determism in the RDO processing.
# Hopefully a fix will be forthcoming.
$toktx --test --genmipmap --threads 1 --uastc --uastc_rdo_l 4 --zcmp 5 ktx_document_uastc_rdo4_zstd5.ktx2 $ktxspec_dir/icons/png/ktx_document.png

